# Paper: Is it still the economy? Economic voting in polarized politics
# County-level model
# Replication: Figure S17 (Supplementary Material)
remove(list = ls())

library(conflicted)
library(tidyverse)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(dplyr)
library(here)
conflict_prefer("here", "here")
library(systemfit)
library(fixest)

load(here("Final_Paper", "R&R", "CountyLevel.RData"))



summary(fe_model2 <- feols(inc_share ~ unemp_rate*elitepolar_house + 
                             log(defl_pcincome) +
                             share_black + share_hisp + share_asian + 
                             share_young + share_elder + log(tot_pop) + 
                             log1p(share_college) + log1p(share_manuf) +
                             as.factor(rural_urban) + state_partiesdiff + 
                             rep_inc + incumbent_cand | county,
                           data = model_data))

(fe_inc <- cbind(Estimate = coef(fe_model2), confint(fe_model2, level = 0.95)))
fe_inc


fe_inc[1,]
fe_inc[24,]

summary(fe_model2)
length(fe_model2$coefficients)
fe_model2$coefficients[1]
fe_model2$coefficients[24]

olsunem <- fe_model2$coefficients[1] #change in unemployment rate
olsint <- fe_model2$coefficients[24] #interaction

olsunem <- fe_inc[1,] #change in unemployment rate
olsint <- fe_inc[24,] #interaction

variables <- c("Unemp.", "Unemp.*Polarization")
ols_results <- rbind(olsunem, olsint)
glimpse(ols_results)

fe_data <- data.frame(variables, ols_results) %>% 
  mutate(model = "FE") %>% 
  mutate(equation = "Vote Share")
glimpse(fe_data)

fe_model2$se[1]
fe_model2$se[24]  
se_unemp <- fe_model2$se[1]
se_int <- fe_model2$se[24]  
se_ols <- rbind(se_unemp, se_int)
se_ols
fe_data <- cbind(fe_data, se_ols)
glimpse(fe_data)

fe_data <- fe_data %>% 
  rename("se_ols" = "unemp_rate")

vcov(fe_model2)
vcov(fe_model2)[1, 1]
vcov(fe_model2)[1, 24]
sqrt(vcov(fe_model2)[1, 1])
sqrt(vcov(fe_model2)[24, 24])
cov_unemp <- vcov(fe_model2)[1, 24]


fe_data <- fe_data %>% 
  mutate(covb = ifelse(variables == "Unemp.*Polarization", cov_unemp, 0))

fe_data05 <- fe_data %>% 
  mutate(Estimate = ifelse(variables == "Unemp.*Polarization",
                           0.35*Estimate, Estimate)) %>% 
  mutate(polarization = 0.35) 

fe_data06 <- fe_data %>% 
  mutate(Estimate = ifelse(variables == "Unemp.*Polarization",
                           0.6*Estimate, Estimate)) %>% 
  mutate(polarization = 0.6) 
fe_data08 <- fe_data %>% 
  mutate(Estimate = ifelse(variables == "Unemp.*Polarization",
                           0.85*Estimate, Estimate)) %>% 
  mutate(polarization = 0.85)

fe_data <- fe_data %>% 
  mutate(polarization = 1)

fe_data <- rbind(fe_data, fe_data05, fe_data06, fe_data08)
glimpse(fe_data)

fe_data <- fe_data %>% 
  mutate(varb = ifelse(variables == "Unemp.",
                       se_ols^2, polarization^2*se_ols^2)) 
glimpse(fe_data)

fe_data2 <- fe_data %>% 
  mutate(variables = ifelse(variables == "Unemp.", "Unemp.*Polarization", variables)) %>% 
  group_by(equation, polarization, variables) %>% 
  summarise(Estimate = sum(Estimate),
            vars = sum(varb),
            covb = sum(covb)) %>% 
  ungroup()
glimpse(fe_data2)

# standard error of marginal effects
fe_data2 <- fe_data2 %>% 
  mutate(se = sqrt(vars + 2*polarization*covb))
fe_data2 <- fe_data2 %>% 
  select(equation:Estimate, se)
glimpse(fe_data2)

glimpse(fe_data)
fe_data <- fe_data %>% 
  filter(polarization == 1) %>% 
  filter(variables == "Unemp.") %>% 
  mutate(polarization = 0)
fe_data <- fe_data %>% 
  select(equation, variables, polarization, Estimate, se = se_ols)

fe_data <- rbind(fe_data, fe_data2)
glimpse(fe_data)

fe_data <- fe_data %>% 
  mutate(se = ifelse(is.na(se), 0.001, se)) %>% 
  mutate(icii = Estimate+(1.96*se)) %>% 
  mutate(ici = Estimate-(1.96*se))

sd(model_data$unemp_rate)
mean(model_data$unemp_rate)

sd(model_data$unemp_rate) #2.67
fe_data <- fe_data %>% 
  mutate(mean_unemp = mean(model_data$unemp_rate)) %>% 
  mutate(sd_uenmp = sd(model_data$unemp_rate)) %>% 
  mutate(estimate_mean_unemp = sd_uenmp*Estimate) %>% 
  mutate(icii_new = estimate_mean_unemp+(1.96*se)) %>% 
  mutate(ici_new = estimate_mean_unemp-(1.96*se))
glimpse(fe_data)


fe_data <- fe_data %>%
  mutate(polarization = as.factor(polarization)) %>%  
  mutate(polarization = factor(polarization, 
                               levels = c("0",
                                          "0.35",
                                          "0.6",
                                          "0.85",
                                          "1"))) %>% 
  mutate(equation = "Incumbent Party")

fig_fe <- fe_data %>% 
  filter(polarization == "0.6" | polarization == "0.85") %>% 
  #filter(variables == "Unemp.Rate" | variables == "Unemp.Rate*Polarization") %>% 
  ggplot(aes(color = polarization, shape = polarization, fill = polarization)) +
  geom_hline(yintercept = 0, colour = "gray37", lty = 2, size = .75) +
  geom_pointrange(aes(x = equation, y = estimate_mean_unemp, ymin = ici_new,
                      ymax = icii_new),
                  lwd = 0.8, position = position_dodge(width = .5)) + 
  theme_bw() + 
  #ggtitle("(c) Model with County Fixed Effects") +
  ylab("Change in Vote Share") + xlab(NULL) + #ylim(-0.1, 0.05) +
  scale_color_manual(name = "Polarization:",
                     values = c("#009999", "#990000"), 
                     labels = c("0.60", "0.85")) +
  scale_shape_manual(name = "Polarization:",
                     values = c(24, 25), 
                     labels = c("0.60", "0.85")) +
  scale_fill_manual(name = "Polarization:",
                    values = c("#009999", "#990000"), 
                    labels = c("0.60", "0.85")) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 14),
        legend.text = element_text(size = 14),
        legend.title = element_text(size = 14),
        legend.position = "bottom",
        legend.direction = "horizontal",
        title = element_text(size = 14))
fig_fe






